<?php 
	if(!function_exists('upload')){
		function upload($name,$size=1048576,$arr=array('jpg','png','gif'),$dir='uploads'){
			if($_FILES[$name]['error']>0){
				switch($_FILES[$name]['error']){
					case 1:
						echo "文件超过2M";die();
						break;
					case 2:
						echo "文件超出MAX_FILE_SIZE大小";
						die();
						break;
					case 3:
						echo "文件上传失败";
						die();
						break;
					case 4:
						echo '请选择文件';
						die();
						break;
					default:
						echo '未知错误';
						die();
						break;
				}
			}

			if($_FILES[$name]['size']>$size){
				echo '上传文件超出指定大小';
			}

			$ext = pathinfo($_FILES[$name]['name'],PATHINFO_EXTENSION);
			if(!in_array($ext,$arr)){
				echo '请上传指定文件类型';
				exit;
			}

			
			$tmp = md5(time());
			for($i=0;$i<4;$i++){
				$dir.='/'.$tmp[$i];
			}
			if(!is_dir($dir)){
				mkdir($dir,0777,true);
			}
			$base = substr($tmp,4);
			$file = $dir.'/'.$base.'.'.$ext;

			if(is_uploaded_file($_FILES[$name]['tmp_name'])){
				move_uploaded_file($_FILES[$name]['tmp_name'], $file);
			}
		}
	}

?>